package JEightTeam;

import java.io.*;

public class TextReaderExt {
	public static final int Max = 9999999;
	//读取文本文家的位置（Url）
	public static final String TxtUrl = "resource/graphInput.txt";
	//读取文本数据是否出错了
	public boolean occurError = false;
	//图的二维数组的存储
	private int[][] GraphArr;
	//开始节点编号，解束节点编号
	private int StartVertex=0,EndVertex=0;
	//顶点个数，边个数
	private int VertexNum=0,EdgeNum = 0;
	//
	//private DataDeal data = new DataDeal();
	 /**
	     * 功能：Java读取txt文件的内容
	     * 步骤：1：先获得文件句柄
	     * 2：获得文件句柄当做是输入一个字节码流，需要对这个输入流进行读取
	     * 3：读取到输入流后，需要读取生成字节流
	     * 4：一行一行的输出。readline()。
	     * 备注：需要考虑的是异常情况
	     * @param filePath
	     */
	public void TextReaderExt_old(){
		try {
			String encoding="UTF-8";
			File file=new File(TxtUrl);
			if(file.isFile() && file.exists()){ //判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file),encoding);//考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				int startV=0,endV=0,distance=0;
				int lineCount = 0,//读取文本的行数
						verNum =0,//顶点个数
						edgeNum =0;//变得个数
				int[][] gArr;//图的二维数组存储
				String[] arrTmp;
				while((lineTxt = bufferedReader.readLine()) != null){
					lineCount ++;
					arrTmp = lineTxt.split(" ");
					//获得顶点和边数
					if(lineCount == 1 && arrTmp.length == 2){
						verNum = Integer.parseInt(arrTmp[0]);
						edgeNum =  Integer.parseInt(arrTmp[1]);
						this.VertexNum = verNum;
						this.EdgeNum = edgeNum;
						//二维数组初始化
						this.GraphArr =  new int[verNum+1][verNum+1];
						//二维数组赋初值－1
						for(int i=1; i<=verNum; i++){
							for(int j=1; j<=verNum; j++){
								this.GraphArr[i][j] = -1;
							}
						}
					}//获得开始顶点编号和终点编号
					else if(arrTmp.length == 2 && lineCount > 1){
						this.StartVertex = Integer.parseInt(arrTmp[0]);
						this.EndVertex = Integer.parseInt(arrTmp[1]);
					}//顶点距离信息
					else{
						startV = Integer.parseInt(arrTmp[0]);
						endV = Integer.parseInt(arrTmp[1]);
						distance = Integer.parseInt(arrTmp[2]);
						//无向图是对称的，所以一条边的长度在矩阵中需要输入两次
						this.GraphArr[startV][endV] = distance;
						this.GraphArr[endV][startV] = distance;
					}
					//输出一行的信息
					//System.out.println(lineTxt);					
				}
				read.close();
				this.occurError = false;
	        }else{
		        System.out.println("找不到指定的文件");
	            this.occurError = true;
	            throw new Exception("找不到指定的文件" + TxtUrl);
	        		 }
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			this.occurError = true;
			e.printStackTrace();	            
		}	     
	}

	public TextReaderExt(){
		try {
			String encoding="UTF-8";
			File file=new File(TxtUrl);
			if(file.isFile() && file.exists()){ //判断文件是否存在
				InputStreamReader read = new InputStreamReader(
						new FileInputStream(file),encoding);//考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				int startV=0,endV=0,distance=0;
				int lineCount = 0,//读取文本的行数
						verNum =0,//顶点个数
						edgeNum =0;//变得个数
				int[][] gArr;//图的二维数组存储
				String[] arrTmp;
				while((lineTxt = bufferedReader.readLine()) != null){
					lineCount ++;
					arrTmp = lineTxt.split(" ");
					//获得顶点和边数
					if(lineCount == 1 && arrTmp.length == 2){
						verNum = Integer.parseInt(arrTmp[0]);
						edgeNum =  Integer.parseInt(arrTmp[1]);
						this.VertexNum = verNum;
						this.EdgeNum = edgeNum;
						//二维数组初始化
						this.GraphArr =  new int[verNum][verNum];
						//二维数组赋初值－1
						for(int n=0; n<verNum; n++){
							for(int j=0; j<verNum; j++){
								this.GraphArr[n][j] = Max;
							}
						}
					}//获得开始顶点编号和终点编号
					else if(arrTmp.length == 2 && lineCount > 1){
						this.StartVertex = Integer.parseInt(arrTmp[0]);
						this.EndVertex = Integer.parseInt(arrTmp[1]);
					}//顶点距离信息
					else{
						startV = Integer.parseInt(arrTmp[0]);
						endV = Integer.parseInt(arrTmp[1]);
						distance = Integer.parseInt(arrTmp[2]);
						//无向图是对称的，所以一条边的长度在矩阵中需要输入两次
						this.GraphArr[startV-1][endV-1] = distance;
						this.GraphArr[endV-1][startV-1] = distance;
					}
					//输出一行的信息
					//System.out.println(lineTxt);					
				}
				read.close();
				this.occurError = false;
	        }else{
		        System.out.println("找不到指定的文件");
	            this.occurError = true;
	            throw new Exception("找不到指定的文件" + TxtUrl);
	        		 }
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			this.occurError = true;
			e.printStackTrace();	            
		}	     
	}
	
	public int getStartVertex(){
		return this.StartVertex;
	}
	
	public int getEndVertex(){
		return this.EndVertex;
	}
	
	public int getVertexNum(){
		return this.VertexNum;
	}
	
	public int getEdgeNum(){
		return this.EdgeNum;
	}
	
	public int[][] getGraphArr(){
		return this.GraphArr;
	}
	/*
	 * 输出二维数组
	 */
	public void PrintGrapharr(){
		System.out.println("顶点个数：" + this.VertexNum + ",边个数：" + this.EdgeNum);
		if(this.GraphArr != null && this.GraphArr.length > 0){
			for(int i=1; i<=this.VertexNum; i++){
				for(int j=1; j<=this.VertexNum; j++){
					System.out.print(this.GraphArr[i][j] + "  ");
				}
				System.out.println();
			}
		}
		System.out.println("开始节点编号：" + this.StartVertex + ",结束节点编号：" + this.EndVertex);
	}
}
